package com.rx.uua.service;

import com.rx.core.base.RxBaseData;
import com.rx.core.data.RxDataScanner;
import com.rx.core.dbase.RxBaseDbMust;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_STATUS;
import com.rx.core.enm.RX_VALID;
import com.rx.core.service.RxBaseAbstractService;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.school.vo.SchMenuInfo;
import com.rx.uua.vo.UuaLocalInfo;
import com.rx.uua.vo.UuaOrgInfo;
import com.rx.uua.vo.common.DictVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 对外公开。无需登录接口
 */
@Service
@Slf4j
public class UuaCommonService extends RxBaseAbstractService {
    public static String TOP_ORG = "0";
    @Autowired
    StandardService standardService;


    public Object topOrgSearch(UuaOrgInfo param) throws IllegalAccessException, InstantiationException {
        UuaOrgInfo uuaOrgInfo = (UuaOrgInfo) RxBaseData.create(UuaOrgInfo.class, HANDLER_TYPE.SINGLE_PAGE);
        RxUtil.copyProperties(param,uuaOrgInfo);
        // 去掉所有权限。
        uuaOrgInfo.getHandlerController().onlyDefault();
        uuaOrgInfo.setClassCode(TOP_ORG);
        uuaOrgInfo.setStatus(RX_STATUS.NORMAL.getCode());

        return standardService.searchPage(uuaOrgInfo);
    }
    public Object dict(List<DictVo> dictVoList) throws IllegalAccessException, InstantiationException, NoSuchFieldException {

        RxUtil.CheckUtil.check(dictVoList, RESULT_CODE.MESSAGE,"查询参数为空");

        // 先查redis缓存

        // 查询数据库。
        for (DictVo dictVo : dictVoList) {

            Class<?> cls = RxDataScanner.getClassByName(dictVo.getTableKey());
            if (EmptyChecker.isEmpty(cls)){
                continue;
            }

            RxBaseDbMust uuaOrgInfo = (RxBaseDbMust) RxBaseData.create(cls, HANDLER_TYPE.SINGLE_PAGE);
            if(EmptyChecker.notEmpty(dictVo.getClassKey())&&EmptyChecker.notEmpty(dictVo.getClassValue())){
                RxUtil.Reflection.setFieldValue(uuaOrgInfo,dictVo.getClassKey(),dictVo.getClassValue());
            }
            uuaOrgInfo.setValid(RX_VALID.NORMAL.getCode());
            uuaOrgInfo.conditionMap().put("in" + RxUtil.StringUtil.firstUpper(dictVo.getFieldKey()),dictVo.getKeyList());
            uuaOrgInfo.getHandlerController().onlyDefault();
            Object object = standardService.searchList(uuaOrgInfo);
            List<RxBaseData> uuaOrgInfoList = RxUtil.result(object);
            if(EmptyChecker.notEmpty(uuaOrgInfoList)){
                for (RxBaseData orgInfo : uuaOrgInfoList) {
                    RxUtil.Reflection.holdValue(orgInfo,dictVo.getValueKeyList());
                }

                dictVo.setValueList(uuaOrgInfoList);
            }
        }



        return dictVoList;
    }

    public Object localList(@RequestBody UuaLocalInfo param){

        try {
            UuaLocalInfo uuaLocalInfo = (UuaLocalInfo)UuaLocalInfo.create(UuaLocalInfo.class,HANDLER_TYPE.SINGLE_LIST);
            RxUtil.copyProperties(param,uuaLocalInfo);
            uuaLocalInfo.getHandlerController().onlyDefault();
            return standardService.searchList(uuaLocalInfo);


        } catch (IllegalAccessException  | InstantiationException e) {

        }

        RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL);
        return null;
    }
}
